package MyThread_demo.shendu_study_ReentrantLock;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author ChenYongHao
 * @date 2023/5/4 14:23
 * @desc //ReentrantLock 支持可打断，如果是不可中断模式，即使使用了 interrupt 也不会让等待中断。
 */
public class 可打断 {
    public static ReentrantLock lock = new ReentrantLock();

    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            System.out.println("t1启动...");
            try {
                lock.lockInterruptibly();
            } catch (InterruptedException e) {
                e.printStackTrace();
                System.out.println("t1等锁的过程中被打断");
                return;
            }
            try {
                System.out.println("t1获得了锁");
            } finally {
                lock.unlock();
            }
        }, "t1");
        lock.lock();
        System.out.println("main获得了锁");
        t1.start();
        try {
            Thread.sleep(1000);
            t1.interrupt();
            System.out.println("main执行打断");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}
